import tkinter as tk
from tkinter import ttk
import requests
import json

class SugarReductionApp:
    def __init__(self, root):
        self.root = root
        self.root.title("果糖含量检测与减糖预估")
        self.root.geometry("800x600")
        self.create_widgets()

    def create_widgets(self):
        ttk.Label(self.root, text="果汁种类:").grid(column=0, row=0, padx=10, pady=5)
        self.juice_var = tk.StringVar()
        ttk.Entry(self.root, textvariable=self.juice_var).grid(column=1, row=0, padx=10, pady=5)

        ttk.Label(self.root, text="果糖含量 (g):").grid(column=0, row=1, padx=10, pady=5)
        self.fructose_var = tk.StringVar()
        ttk.Entry(self.root, textvariable=self.fructose_var).grid(column=1, row=1, padx=10, pady=5)

        ttk.Label(self.root, text="预估减糖后的果糖含量 (g):").grid(column=0, row=2, padx=10, pady=5)
        self.reduced_fructose_var = tk.StringVar()
        ttk.Entry(self.root, textvariable=self.reduced_fructose_var).grid(column=1, row=2, padx=10, pady=5)

        ttk.Label(self.root, text="预估减糖时间 (分钟):").grid(column=0, row=3, padx=10, pady=5)
        self.reduction_time_var = tk.StringVar()
        ttk.Entry(self.root, textvariable=self.reduction_time_var).grid(column=1, row=3, padx=10, pady=5)

        ttk.Label(self.root, text="用户身高 (cm):").grid(column=0, row=4, padx=10, pady=5)
        self.height_var = tk.StringVar()
        ttk.Entry(self.root, textvariable=self.height_var).grid(column=1, row=4, padx=10, pady=5)

        ttk.Label(self.root, text="用户体重 (kg):").grid(column=0, row=5, padx=10, pady=5)
        self.weight_var = tk.StringVar()
        ttk.Entry(self.root, textvariable=self.weight_var).grid(column=1, row=5, padx=10, pady=5)

        ttk.Label(self.root, text="用户性别:").grid(column=0, row=6, padx=10, pady=5)
        self.gender_var = tk.StringVar()
        self.gender_var.set("男")
        ttk.Combobox(self.root, textvariable=self.gender_var, values=["男", "女"]).grid(column=1, row=6, padx=10, pady=5)

        ttk.Button(self.root, text="上传到云", command=self.upload_to_cloud).grid(column=1, row=7, padx=10, pady=10)

        self.log_text = tk.Text(self.root, height=10, width=80)
        self.log_text.grid(column=0, row=8, columnspan=2, padx=10, pady=5)

    def upload_to_cloud(self):
        data = {
            "juice": self.juice_var.get(),
            "fructose": self.fructose_var.get(),
            "reduced_fructose": self.reduced_fructose_var.get(),
            "reduction_time": self.reduction_time_var.get(),
            "height": self.height_var.get(),
            "weight": self.weight_var.get(),
            "gender": self.gender_var.get()
        }

        url = "http://47.109.206.252:5000/upload"  # 使用你的ECS公网IP
        try:
            response = requests.post(url, json=data)
            if response.status_code == 200:
                self.log_text.insert(tk.END, "上传成功！\n")
            else:
                self.log_text.insert(tk.END, f"上传失败: {response.text}\n")
        except requests.exceptions.RequestException as e:
            self.log_text.insert(tk.END, f"请求错误: {e}\n")

if __name__ == "__main__":
    root = tk.Tk()
    app = SugarReductionApp(root)
    root.mainloop()